package com.uit.gis.config;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;

import com.uit.gis.util.Util;

public class DatabaseConfig extends Config {

	public enum DatabaseField {
		SAVE_COMPUTER("saveDatabaseConfig", "false"), HOST_NAME("hostName", ""), PORT(
				"port", ""), SID("sid", ""), USERNAME("username", ""), PASSWORD(
				"password", "");
		String field, defaultValue;

		DatabaseField(String field, String value) {
			// TODO Auto-generated constructor stub
			this.field = field;
			this.defaultValue = value;
		}
	}

	boolean saveOnComputer = false;

	public boolean isSaveOnComputer() {
		return saveOnComputer;
	}

	public void setSaveOnComputer(boolean saveOnComputer) {
		this.saveOnComputer = saveOnComputer;
	}

	public String getHostName() {
		return hostName;
	}

	public void setHostName(String hostName) {
		this.hostName = hostName;
	}

	public String getPort() {
		return port;
	}

	public void setPort(String port) {
		this.port = port;
	}

	public String getsId() {
		return sId;
	}

	public void setsId(String sId) {
		this.sId = sId;
	}

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public String getPassWord() {
		return passWord;
	}

	public void setPassword(String passWord) {
		this.passWord = passWord;
	}

	String hostName = "";
	String port = "";
	String sId = "";
	String userName = "";
	String passWord = "";

	public DatabaseConfig() {
		// TODO Auto-generated constructor stub
		super.fileName = ConfigName.DATABASE.name;
	}

	@Override
	public void readConfigFile() throws IOException {
		// TODO Auto-generated method stub
		Properties applicationProps = new Properties();
		try {
			FileInputStream in = new FileInputStream(fileName);
			applicationProps.load(in);
			saveOnComputer = Boolean.parseBoolean(applicationProps
					.getProperty(DatabaseField.SAVE_COMPUTER.field));
			hostName = applicationProps
					.getProperty(DatabaseField.HOST_NAME.field);
			port = applicationProps.getProperty(DatabaseField.PORT.field);
			sId = applicationProps.getProperty(DatabaseField.SID.field);
			userName = applicationProps
					.getProperty(DatabaseField.USERNAME.field);
			passWord = applicationProps
					.getProperty(DatabaseField.PASSWORD.field); 
			 
			byte[] decryptedStr = Util.aes_Decrypt(Util.keyBytes,  passWord.getBytes());
			passWord = new String(decryptedStr);
			in.close();

		} catch (Exception e) {
			// If property isn't exist , create new property , write default
			// value
			try {
				saveOnComputer = Boolean
						.parseBoolean(DatabaseField.SAVE_COMPUTER.defaultValue);
				hostName = DatabaseField.HOST_NAME.defaultValue;
				port = DatabaseField.PORT.defaultValue;
				sId = DatabaseField.SID.defaultValue;
				userName = DatabaseField.SID.defaultValue;
				passWord = DatabaseField.SID.defaultValue;
				writeConfigFile();
			} catch (IOException ioex) {
				System.err.println(ioex);
			}
		}

	}

	@Override
	public void writeConfigFile() throws IOException {
		// TODO Auto-generated method stub
		FileOutputStream out = new FileOutputStream(fileName);
		Properties applicationPropsOut = new Properties();
		applicationPropsOut.setProperty(DatabaseField.SAVE_COMPUTER.field,
				String.valueOf(saveOnComputer));
		applicationPropsOut
				.setProperty(DatabaseField.HOST_NAME.field, hostName);
		applicationPropsOut.setProperty(DatabaseField.PORT.field, port);
		applicationPropsOut.setProperty(DatabaseField.SID.field, sId);
		applicationPropsOut.setProperty(DatabaseField.USERNAME.field, userName);
		byte[] encrypt = new byte[16];
		try {
			if (passWord.length() % 16 != 0) {
				int encryptStrLength = passWord.length() + 16
						- (passWord.length() % 16);
				encrypt = new byte[encryptStrLength];

				for (int i = 0; i < passWord.length(); i++)
					encrypt[i] = passWord.getBytes()[i];

				for (int i = passWord.length(); i < encryptStrLength; i++)
					encrypt[i] = 0;
			}
			encrypt = Util.aes_Encrypt(Util.keyBytes, encrypt);
		} catch (Exception e) {
			e.printStackTrace();
		}
		applicationPropsOut.setProperty(DatabaseField.PASSWORD.field,
				new String(encrypt)); // use aes later
		applicationPropsOut.store(out, fileName);
		out.close();
	}

}
